﻿/*
 * StringTokenizer.java
 *
 * Created on 2007年7月12日, 下午 3:22
 *
 */

package org.uranus.core.uitl;

import java.util.Vector;

/**
 * Copyright 2007 Hsiang-Yu Wang<br>
 * This file is distributed under the terms of the GNU General Public License<br>
 *<br>
 *<pre>
 * --------------------License notice--------------------
 * GNU GENERAL PUBLIC LICENSE
 * Version 3, 29 June 2007
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not,
 * visit: http://www.gnu.org/licenses/gpl.txt
 * --------------------License notice--------------------</pre>
 *
 * @author Hsiang-Yu Wang
 */
/**
 * 此class能將String依照特定char做分割, 與SE中的StringTokenizer功能類似.
 */
public class StringTokenizer {
    private String incomming;
    private char seper;
    private Vector vector;
    private int index;
    /** Creates a new instance of StringTokenizer */
    public StringTokenizer() {
        incomming = "";
        vector = new Vector();
    }
    /**
     * @param incomming
     *        須被分解的字串原型
     * @param seper
     *        分解的基準character
     */
    public StringTokenizer(String incomming, char seper){
        this.incomming = incomming;
        this.seper = seper;
        vector = new Vector();
        index = 0;
        findTokens();
    }
    
    private void findTokens(){
        char[] temp = incomming.toCharArray();
        int j = 0;
        String combine = "";
        for(int i = 0 ; i < temp.length ; i++){
            if(temp[i] == seper){
                for(int k = j ; k < i ; k++){
                    combine = combine + temp[k];
                }
                vector.addElement(combine);
                combine = "";
                j = i + 1;
            }else if(i == temp.length - 1){
                for(int k = j ; k < temp.length ; k++){
                    combine = combine + temp[k];
                }
                vector.addElement(combine);
                combine = "";
            }
        }
    }
    /**
     *  如還有剩下尚未被讀取出的token(s)則會回傳true, and vice versa.
     */
    public boolean hasMoreTokens(){
        if(vector.isEmpty())
            return false;
        return true;
    }
    
    
    public String nextToken(){
        String temp = (String)vector.firstElement();
        vector.removeElementAt(0);
        return temp;
        
    }
}
